/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.netcracker.test.nine;
import java.util.Iterator;
import java.util.ListIterator;
/**
 *
 * @author I
 */
public class MyListStr implements Iterable<Character> {
    private Character array[];
    public MyListStr(String s) {
        array= new Character[s.length()];
        for (int i = 0; i < s.length(); i++) 
        {
            array[i]=s.charAt(i);
        }
        
    }
    @Override
    public Iterator<Character> iterator() {
        return new MyIterator();
    }
    private class MyIterator implements ListIterator<Character>
    {
        private int current = -1;

        @Override
        public boolean hasNext() {
            return current + 1 < array.length;
        }

        @Override
        public Character next() {
            return array[++current];
                   
        }

        @Override
        public boolean hasPrevious() {
            return array.length != 0 && current - 1 > -1;
        }

        @Override
        public Character previous() {
            return array[--current];
        }

        @Override
        public int nextIndex() {
            return current + 1;
        }

        @Override
        public int previousIndex() {
            return current - 1;
        }

        @Override
        public void remove() {
            Character old[] = array;
            array = new Character[old.length - 1];
            System.arraycopy(old, 0, array, 0, current);
            System.arraycopy(old, current + 1, array, current, array.length - current);
            --current;
        }

        @Override
        public void set(Character element) {
            array[current] = element;
        }

        @Override
        public void add(Character element) {
            int position = current == -1 ? 0 : current;
            Character old[] = array;
            array = new Character[old.length + 1];
            System.arraycopy(old, 0, array, 0, position);
            System.arraycopy(old, position, array, position + 1, old.length - position);
            array[position] = element;
        }        
    }
    
}
